package cn.edu.csust.coursecard.common;

import cn.edu.csust.coursecard.annotation.ClusterTask;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

@Component
public class PageGetter {
    private final OkHttpClient okHttpClient;

    public PageGetter() {
        this.okHttpClient = new OkHttpClient();
    }

    @ClusterTask
    public String getCoursePage(String cookie, String term, int week) throws IOException {
        String url = "http://xk.csust.edu.cn/jsxsd/xskb/xskb_list.do";
        FormBody formBody;
        if (week == 0) {
            formBody = new FormBody.Builder()
                    .add("xnxq01id", term)
                    .build();
        } else {
            formBody = new FormBody.Builder()
                    .add("xnxq01id", term)
                    .add("zc", week + "")
                    .build();
        }
        Request getCourse = new Request.Builder()
                .url(url)
                .header("Cookie", cookie)
                .post(formBody)
                .build();

        Response response = null;
        response = okHttpClient.newCall(getCourse).execute();
        String htmlText = Objects.requireNonNull(response.body()).string();
        response.close();
        return htmlText;
    }


    public String getWeekCoursePage(String cookie, String weekTime) throws IOException {
        String url = "http://xk.csust.edu.cn/jsxsd/framework/main_index_loadkb.jsp";

        FormBody formBody = new FormBody.Builder()
                .add("rq", weekTime)
                .build();

        Request kbRequest = new Request.Builder()
                .header("Cookie", cookie)
                .header("Referer", "http://xk.csust.edu.cn/jsxsd/framework/xsMain_new.jsp?t1=1")
                .header("Origin", "http://xk.csust.edu.cn")
                .url(url)
                .post(formBody)
                .build();

        Response response = null;
        response = okHttpClient.newCall(kbRequest).execute();
        String htmlText = Objects.requireNonNull(response.body()).string();
        response.close();
        return htmlText;
    }


    public String getExamPlansPage(String cookie, String term) throws IOException {
        String url = "http://xk.csust.edu.cn/jsxsd/xsks/xsksap_list";
        FormBody formBody = new FormBody.Builder()
                .add("xqlbmc", "")
                .add("xnxqid", term)
                .add("xqlb", "")
                .build();
        Request getKsapRequest = new Request.Builder()
                .url(url)
                .header("cookie", cookie)
                .header("Referer", "http://xk.csust.edu.cn/jsxsd/xsks/xsksap_query")
                .post(formBody)
                .build();
        Response response = null;

        response = okHttpClient.newCall(getKsapRequest).execute();
        String htmlText = Objects.requireNonNull(response.body()).string();
        response.close();
        return htmlText;
    }


    public String getScorePage(String cookie, String term) throws IOException {
        String url = "http://xk.csust.edu.cn/jsxsd/kscj/cjcx_list";
        FormBody formBody = new FormBody.Builder()
                .add("kksj", term)
                .add("kcxz", "")
                .add("kmmc", "")
                .add("xsfs", "all")
                .add("fxkc", "2")
                .build();
        Request queryScoreRequest = new Request.Builder()
                .url(url)
                .header("Cookie", cookie)
                .header("Referer", "http://xk.csust.edu.cn/jsxsd/kscj/cjcx_query")
                .post(formBody)
                .build();

        Response response = null;
        response = okHttpClient.newCall(queryScoreRequest).execute();
        String htmlText = Objects.requireNonNull(response.body()).string();
        response.close();
        return htmlText;
    }


    public String getScoreDetailPage(String cookie, String pscjUrl) throws IOException {
        Request jwPscjQueryRequest = new Request.Builder()
                .header("Cookie", cookie)
                .url("http://xk.csust.edu.cn" + pscjUrl)
                .build();

        Response response = null;
        response = okHttpClient.newCall(jwPscjQueryRequest).execute();
        String htmlText = Objects.requireNonNull(response.body()).string();
        response.close();
        return htmlText;
    }


    public String getNoticeListPage(String cookie) throws IOException {
        String url = "http://xk.csust.edu.cn/jsxsd/ggly/ysgg_query";
        Request request = new Request.Builder()
                .header("Cookie", cookie)
                .header("Host", "xk.csust.edu.cn")
                .header("Referer", "http://xk.csust.edu.cn/jsxsd/framework/xsMain.jsp")
//                .header("Referer", "http://xk.csust.edu.cn/jsxsd/framework/xsMain.jsp")
                .url(url)
                .get()
                .build();

        Response response = null;
        response = okHttpClient.newCall(request).execute();
        String htmlText = Objects.requireNonNull(response.body()).string();
        response.close();
        return htmlText;
    }


    public String getNoticeDetailPage(String cookie, String ggid) throws IOException {
        String baseUrl = "http://xk.csust.edu.cn/jsxsd/ggly/ggly_show?ggid=";
        String url = baseUrl + ggid;
        Request request = new Request.Builder()
                .header("Cookie", cookie)
                .header("Host", "xk.csust.edu.cn")
                .header("Referer", "http://xk.csust.edu.cn/jsxsd/framework/xsMain.jsp")
//                .header("Referer", "http://xk.csust.edu.cn/jsxsd/framework/xsMain.jsp")
                .url(url)
                .get()
                .build();

        Response response = null;
        response = okHttpClient.newCall(request).execute();
        String htmlString = Objects.requireNonNull(response.body()).string();
        response.close();
        return htmlString;
    }


    public String getSemesterPage(String cookie) throws IOException {
        String url = "http://xk.csust.edu.cn/jsxsd/xskb/xskb_list.do";
        Request getXueqi = new Request.Builder()
                .url(url)
                .header("Cookie", cookie)
                .build();

        Response response = null;
        response = okHttpClient.newCall(getXueqi).execute();
        String htmlText = Objects.requireNonNull(response.body()).string();
        response.close();
        return htmlText;
    }

    @ClusterTask
    public String getWeekDatePage(String cookie) throws IOException {
        String url = "http://xk.csust.edu.cn/jsxsd/framework/xsMain_new.jsp?t1=1";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request xsMainRequest = new Request.Builder()
                .header("Cookie", cookie)
                .header("Referer", "http://xk.csust.edu.cn/jsxsd/framework/xsMain.jsp")
                .url(url)
                .build();

        Response response = null;
        response = okHttpClient.newCall(xsMainRequest).execute();
        String htmlText = Objects.requireNonNull(response.body()).string();
        response.close();
        return htmlText;
    }

}